package com.zhouzi.zhaoyang.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.IBinder
import android.util.Log
import com.qq.wx.voice.synthesizer.SpeechSynthesizer
import com.qq.wx.voice.synthesizer.SpeechSynthesizerListener
import com.qq.wx.voice.synthesizer.SpeechSynthesizerResult
import com.qq.wx.voice.synthesizer.SpeechSynthesizerState
import com.zhouzi.zhaoyang.util.Player
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

/**
 * Created by ZhouZi on 2019/7/12.
 * time:11:18
 * ----------Dragon be here!----------/
 * 　　　┏┓　　 ┏┓
 * 　　┏┛┻━━━┛┻┓━━━
 * 　　┃　　　　　 ┃
 * 　　┃　　　━　  ┃
 * 　　┃　┳┛　┗┳
 * 　　┃　　　　　 ┃
 * 　　┃　　　┻　  ┃
 * 　　┃　　　　   ┃
 * 　　┗━┓　　　┏━┛Code is far away from bug with the animal protecting
 * 　　　　┃　　　┃    神兽保佑,代码无bug
 * 　　　　┃　　　┃
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛━━━━━
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━━━━━━神兽出没━━━━━━━━━━━━━━
 */
class VoiceService : Service() {
    override fun onBind(intent: Intent?): IBinder? = null

    val player = Player()

    override fun onCreate() {
        super.onCreate()
        initTTS(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {

        if (intent != null) {
            val voiceStr = intent.getStringExtra("voiceStr")
            SpeechSynthesizer.shareInstance().start(voiceStr)

        }

        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        super.onDestroy()
        SpeechSynthesizer.shareInstance().destroy()
    }

    private fun initTTS(context: Context) {
        SpeechSynthesizer.shareInstance().setListener(object : SpeechSynthesizerListener {
            override fun onGetResult(speechSynthesizerResult: SpeechSynthesizerResult) {
                Log.e("LoginActivity", "onGetResult")
                val speech = speechSynthesizerResult.speech
                val filepath = context.getExternalFilesDir("收钱语音")!!.toString()
                val outputpath = File(filepath)
                if (!outputpath.exists()) {
                    outputpath.mkdirs()
                }
                val voiceType = ".mp3"
                val voiceFileName = "$filepath/收钱语音$voiceType"
                val voiceFile = File(voiceFileName)
                if (!voiceFile.exists()) {
                    try {
                        voiceFile.createNewFile()
                    } catch (e: IOException) {
                        // TODO Auto-generated catch block
                        e.printStackTrace()
                    }

                }
                var voiceOutputStream: FileOutputStream? = null
                try {
                    voiceOutputStream = FileOutputStream(voiceFile)
                } catch (e1: FileNotFoundException) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace()
                }

                try {
                    voiceOutputStream!!.write(speech)
                    voiceOutputStream.close()
                } catch (e: IOException) {
                    // TODO Auto-generated catch block
                    e.printStackTrace()
                }

                player.playFile(voiceFileName)
            }

            override fun onGetError(i: Int) {
                Log.e("LoginActivity", "ERRORCODE=$i")
            }

            override fun onGetVoiceRecordState(speechSynthesizerState: SpeechSynthesizerState) {

            }
        })
        SpeechSynthesizer.shareInstance().setFormat(0)
        SpeechSynthesizer.shareInstance().setVolume(1.0f)
        SpeechSynthesizer.shareInstance().init(context, "wxd0fa8b1998064f7d")
    }

}